package com.szholly.plug.activity.interceptor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 工作流拦截注册器
 */
public class BpmInterceptorRegister {
	
	/**
	 * 流程处理(通过，不通过)通知事件
	 */
	public static List<IBpmHandleOption> bpmHandleOption = new ArrayList<IBpmHandleOption>();
	
	/**
	 * 工作流等待事件接口
	 */
	public static List<IBpmWaitOption> bpmWaitOption = new ArrayList<IBpmWaitOption>();

	/**
	 * 注册编辑拦截接口
	 * @param editInterceptor
	 */
	public static void RegDbInterceptor(IBpmOption editInterceptor) {
		if (editInterceptor == null)
			return;
		
		if (editInterceptor instanceof IBpmHandleOption) {
			if (!bpmHandleOption.contains((IBpmHandleOption) editInterceptor)) {
				bpmHandleOption.add((IBpmHandleOption) editInterceptor);
				Collections.sort(bpmHandleOption, comparator);
			}
		}
		
		if (editInterceptor instanceof IBpmWaitOption) {
			if (!bpmWaitOption.contains((IBpmWaitOption) editInterceptor)) {
				bpmWaitOption.add((IBpmWaitOption) editInterceptor);
				Collections.sort(bpmWaitOption, comparator);
			}
		}
	}

	/**
	 * 移除编辑拦截接口
	 * @param editInterceptor
	 */
	public static void RemoveDbInterceptor(IBpmOption editInterceptor) {
		if (editInterceptor == null)
			return;

		if (editInterceptor instanceof IBpmHandleOption) {
			if (bpmHandleOption.contains((IBpmHandleOption) editInterceptor)) {
				bpmHandleOption.remove((IBpmHandleOption) editInterceptor);
				Collections.sort(bpmHandleOption, comparator);
			}
		}
		
		if (editInterceptor instanceof IBpmWaitOption) {
			if (bpmWaitOption.contains((IBpmWaitOption) editInterceptor)) {
				bpmWaitOption.remove((IBpmWaitOption) editInterceptor);
				Collections.sort(bpmWaitOption, comparator);
			}
		}
	}

	private static Comparator<IBpmOption> comparator = new Comparator<IBpmOption>() {
		public int compare(IBpmOption a, IBpmOption b) {
			if (a.getIndex() - b.getIndex() >= 0) {
				return 1;
			} else {
				return -1;
			}
		}
	};
}